package com.imooc.mail.service.impl;

import com.imooc.mail.consts.MallConst;
import com.imooc.mail.dao.CategoryMapper;
import com.imooc.mail.pojo.Category;
import com.imooc.mail.pojo.CategoryExample;
import com.imooc.mail.service.ICategoryService;
import com.imooc.mail.vo.CategoryVo;
import com.imooc.mail.vo.ResponseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author wjc
 * Date on 2020/6/6 16:25
 */
@Service
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public ResponseVo<List<CategoryVo>> selectAll() {
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andStatusEqualTo(true);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);

//        List<CategoryVo> categoryVoList = new ArrayList<>();
        // 1.for循环
//        for (Category category : categories){
//            if (category.getId().equals(MallConst.CATEGORY_PARENT_ROOT)){
//                CategoryVo categoryVo = new CategoryVo();
//                BeanUtils.copyProperties(category,categoryVo);
//                categoryVoList.add(categoryVo);
//            }
//        }



        // 2.lambda表达式
        List<CategoryVo> categoryVoList = categories.stream()
                .filter(e -> e.getId().equals(MallConst.CATEGORY_PARENT_ROOT))
                .map(this::category2CategoryVo)
                .sorted(Comparator.comparing(CategoryVo::getSortOrder).reversed())
                .collect(Collectors.toList());

        // 查询子目录
        findSubCategory(categoryVoList, categories);

        return ResponseVo.success(categoryVoList);
    }

    @Override
    public void findSubCategoryId(Integer id, Set<Integer> resultSet) {
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andStatusEqualTo(true);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        for (Category category : categories) {
            if (category.getParentId().equals(id)){
                resultSet.add(category.getId());
                // 重载
                findSubCategoryId(category.getId(), resultSet, categories);
            }
        }
    }

    private void findSubCategory(List<CategoryVo> categoryVoList, List<Category> categories) {
        for (CategoryVo categoryVo : categoryVoList) {
            List<CategoryVo> subCategoryVoList = new ArrayList<>();
            for (Category category : categories) {
                // 如果查到内容，设置subCategory， 继续往下查
                if (categoryVo.getId().equals(category.getParentId())) {
                    subCategoryVoList.add(category2CategoryVo(category));
                }
                subCategoryVoList.sort(Comparator.comparing(CategoryVo::getSortOrder).reversed());
                categoryVo.setSubCategories(subCategoryVoList);
                findSubCategory(subCategoryVoList, categories);
            }

        }
    }


    private void findSubCategoryId(Integer id, Set<Integer> resultSet, List<Category> categories) {
        for (Category category : categories) {
            if (category.getParentId().equals(id)) {
                resultSet.add(category.getId());
                findSubCategoryId(category.getId(), resultSet, categories);
            }
        }
    }

    private CategoryVo category2CategoryVo(Category category) {
        CategoryVo categoryVo = new CategoryVo();
        BeanUtils.copyProperties(category, categoryVo);
        return categoryVo;
    }

}
